package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.mapper.AccountMapper;
import cn.wolfcode.p2p.base.service.IAccountService;

import cn.wolfcode.p2p.base.domain.Account;
import java.util.List;
import cn.wolfcode.base.QueryObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AccountMapper accountMapper;
	
	@Transactional(readOnly=true)
	public PageInfo<Account> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<Account> list = accountMapper.selectList(qo);
		return new PageInfo<Account>(list);
	}

	@Override
	public Account get(Long id) {
		return accountMapper.selectById(id);
	}

	@Override
	public void update(Account account) {
		int rows = accountMapper.updateById(account);

		if (rows == 0){
			throw new CustomerException("乐观锁失败, Account.id : " + account.getId());
		}
	}
}
